<?php

class Manager {

	private $data = array();
	
	function __construct ($filename) {
		$this->data['slash'] = '/';
		$this->data['pasta'] = 'save';
		
		if ($this->data['pasta'] != '') $this->data['pasta'] .= $this->data['slash'];
		$this->data['filename'] = $filename;
		$this->data['nome'] = $filename;
		$this->data['obj'] = Object;
		
		// Testar escrita
		$this->writeObj($this, 'filename', 'teste');
		//if (!$this->writeObj($this, 'filename', 'teste')) die('A script terminou: erro no teste de escrita!');
		
		// Testar leitura
		$this->readObj('teste', 'teste');
		//if (!$this->readObj('filename', 'teste')) die('A script terminou: erro no teste de leitura!');
	}
	
	public function writeObj($obj, $chave, $dir) {
		
		if ($dir != '') $dir .= $this->getValue('slash');
		$this->data['filename'] = $this->getValue('pasta') . $dir . $obj->getValue($chave) .'.obj';
		$serialized = serialize($obj);

        //echo "<p>write ". $this->data['filename'] ." ... ";

		if (file_exists($this->getValue('filename'))) {
			if (!$fp = @fopen($this->getValue('filename'), "w")) {
                //echo ' error opening for writing only!</p>';
                return false;
            }
		} else {
			if (!$fp = @fopen($this->getValue('filename'), "x")) {
                //echo ' error creating for writing!</p>';
                return false;
            }
		}

		if (flock($fp, LOCK_EX)) { // do an exclusive lock
		    $fwrite = fwrite($fp, $serialized);
		    flock($fp, LOCK_UN); // release the lock
            if (!$fwrite) {
                //echo ' error writing!</p>';
                return false;
            }
            else {
                //echo $fwrite . ' bytes OK!</p>';
            }
		} else {
                //echo ' error locking for writing!</p>';
                return false;
            }

		if ($fp) fclose($fp);
		return true;
	}
	
	public function readObj($chave, $dir) {
		//echo "<p>read ". $chave ."</p>";
		if ($dir != '') $dir .= $this->getValue('slash');
		$this->data['filename'] = $this->getValue('pasta') . $dir . $chave .'.obj';
		
		if (file_exists($this->getValue('filename'))) {
			if (!$fp = @fopen($this->getValue('filename'), "rb")) return false;
			
			if (flock($fp, LOCK_SH)) { // do an exclusive lock
			    if (!$obj = @fread($fp, filesize($this->getValue('filename')))) return false;
			    flock($fp, LOCK_UN); // release the lock
				
				$tobj = implode("", @file($this->getValue('filename')));
				$obj = unserialize($tobj);
		
			} else return false;

			fclose($fp);
		} else return false;
		
		return $obj;
	}
	
	public function clear($dir) {
		$dirlist = opendir($this->getValue('pasta') . $dir);
		while ($item = readdir($dirlist)) {
			if (!unlink($item)) return false;
		}
		closedir($dirlist);
		if (!mkdir($this->getValue('pasta') . $dir, 0644)) return false;
	}
	
	public function getValue($chave) {
		return $this->data[$chave];
	}
}
?>